package com.xiaoma.controller;

import com.xiaoma.pojo.FileUploadDTO;
import com.xiaoma.pojo.FileUploadRequestDTO;
import com.xiaoma.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;

@Controller
@RequestMapping(value="/")
@Slf4j
public class FileController {


  @Autowired
  private FileService fileService;

  @GetMapping(value="/uploadFile")
  public String gotoFilePage(){
    return "upload";
  }

  @PostMapping(value = "/upload")
  @ResponseBody
  public FileUploadDTO upload(FileUploadRequestDTO fileUploadRequestDTO) throws IOException {
   return fileService.upload(fileUploadRequestDTO);
  }

  @PostMapping(value = "checkFileMd5")
  @ResponseBody
  public FileUploadDTO checkFileMd5(String md5, String path, String name) throws IOException {
     return fileService.checkFileMd5(new FileUploadRequestDTO().setMd5(md5).setPath(path).setName(name));
  }

}
